﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasertTemplates.Master" AutoEventWireup="true"
    CodeBehind="ProductCategory.aspx.cs" Inherits="WebApplication.ProductCategory" %>

<%@ Register TagPrefix="uc1" TagName="usermeg" Src="~/UserControl/UserMessageControl.ascx" %>
<%@ Register TagPrefix="uc1" TagName="maintab" Src="~/UserControl/MainTabControl.ascx" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        $(document).ready(function () {
            $("#menu1_ul1").find("li").each(function (i) {
                $(this).click(function () { $(this).children("span").toggleClass("span" + (i + 1)); });
            });

            $("#menu1_ul1").find(".li1").children("span").addClass("span1");
            $("#div_A").css("display", "block");

            //产品大分类 菜单
            $("#menu1_ul1").find("li").each(function (i) {
                $(this).click(function () {
                    $("#menu1_ul1").find("li").children("span").removeAttr("class");
                    $(this).children("span").addClass("span" + (i + 1));
                    display_productcategory($(this).children("span").attr("title"));
                });
            });


            $.each(['A', 'B', 'C', 'D', 'E'], function (k, v) {
                //各category全选All 事件
                $("#ContentPlaceHolder2_rp_Cate_All_cb_all_" + k).click(function () {
                    if ($(this).attr("checked") == true) {
                        $("#div_" + v).find("input").not($(this)).attr("checked", 'true');
                        //$("#menu1_ul1 .li" + (k + 1) + " .selected").html("√");
                    }
                    else {
                        $("#div_" + v).find("input").not($(this)).attr("checked", '');
                       // $("#menu1_ul1 .li" + (k + 1) + " .selected").html("");
                    }
                });


                /////category 事件
                $("#ContentPlaceHolder2_rp_Cate_All_cbl_cate_" + k + " td input").click(function () {
                    var source = $(this).attr("id").toString();
                    var p = source[41];
                    var c = source[43];

                    if ($(this).attr("checked") == true) {
                        $("#ContentPlaceHolder2_rp_Cate_All_rp_cate_" + p + "_cbl_subcate_" + c + " input").attr("checked", 'true');
                    }
                    else {
                        $("#ContentPlaceHolder2_rp_Cate_All_rp_cate_" + p + "_cbl_subcate_" + c + " input").attr("checked", '');
                    }
                    SubItemCheck(k);
                });



                ///subcategory 事件
                $("#div_" + v).find("input").not("#ContentPlaceHolder2_rp_Cate_All_cb_all_" + k).not("#ContentPlaceHolder2_rp_Cate_All_cbl_cate_" + k + " input").click(function () {
                    SubItemCheck(k);
                    // hasChSelect();
                });

                $(".cate_and_sub").find("input[type='checkbox']").click(function () {
                    hasChSelect();
               })
               

                SubItemCheck(k);
              
            });

            hasChSelect();
        });


         //根据子项选中情况 绑定父项（所以的子项全选的时候 父项选中，子项任何一个未选父项设为未选）！
        function SubItemCheck(mainItemIndex) {
            var catagoryIndex = $("#ContentPlaceHolder2_rp_Cate_All_cbl_cate_" + mainItemIndex + " td").find("input").length;  
            for (i = 0; i < catagoryIndex; i++) {
                var subcategory = $("#ContentPlaceHolder2_rp_Cate_All_rp_cate_"+mainItemIndex+"_cbl_subcate_" + i + " td").find("input"); //ContentPlaceHolder2_rp_Cate_All_rp_cate_0_cbl_subcate_1
                var allChildChecked = true;
              
                $.each(subcategory, function (j) {
                    if (subcategory.eq(j).attr("checked") != true) {
                        allChildChecked = false;
                        return false;
                    }
                    else {                      
                        return true;
                    }
                });
                

                if (allChildChecked == true) {
                    $("#ContentPlaceHolder2_rp_Cate_All_cbl_cate_" + mainItemIndex + "_" + i + "_" + mainItemIndex).attr("checked", 'true');  //ContentPlaceHolder2_rp_Cate_All_cbl_cate_0_1_0
                }
                else {
                    $("#ContentPlaceHolder2_rp_Cate_All_cbl_cate_" + mainItemIndex + "_" + i + "_" + mainItemIndex).attr("checked", '');
                }
            }


            var allParentChecked = true;
            var tmp_category_item = $("#ContentPlaceHolder2_rp_Cate_All_cbl_cate_" + mainItemIndex + " td").find("input");
            $.each(tmp_category_item, function (i) {
                if (tmp_category_item.eq(i).attr("checked") != true) {
                    allParentChecked = false;
                    return false;
                }
                else {
                    return true;
                }
            });
            if (allParentChecked == true) {
                $("#ContentPlaceHolder2_rp_Cate_All_cb_all_" + mainItemIndex).attr("checked", 'true');
               // $("#menu1_ul1 .li" + (mainItemIndex+1) + " .selected").html("√");
            }
            else {
                $("#ContentPlaceHolder2_rp_Cate_All_cb_all_" + mainItemIndex).attr("checked", '');
              //  $("#menu1_ul1 .li" + (mainItemIndex+1) + " .selected").html("");
            }

        }


        //隐藏所有 显示特定的
        function display_productcategory(i) {
            $("#id_check_container").find(".cate_and_sub").css("display", "none");
            $("#div_" + i).css("display", "block");
        }

        function hasChSelect() {
            $("#id_check_container").find(".cate_and_sub").each(function (i) {

                var has = false;
                $(this).find("input[type='checkbox']").each(function () {
                    if ($(this).attr('checked')) {
                        has = true;
                        return false;
                    }
                })


                if (has) 
                {
                   
                    $("#menu1_ul1").find(".li" + (i + 1)).find(".selected").html("√")
                    
                }
                else
                  $("#menu1_ul1").find(".li" + (i + 1)).find(".selected").html(" ");

            })
        
        }


        function GetChangeVal() {
            var addVal = "";
            var proVal = "";
            var delVal = "";
            var hiddenStr = $("#ContentPlaceHolder2_HiddenField_val").val();
            var hiddenVal = hiddenStr.substring(0, hiddenStr.length - 1).split(",");

            $("div").find(".ProCat").find("input:checked").each(function (m, n) {
                var has = true;
                for (j = 0; j < hiddenVal.length; j++) {
                    if (n.value == hiddenVal[j]) {
                        has = false;
                        break;
                    }
                }
                if (has) {
                    addVal += n.value + ",";
                }
                else {
                    proVal += n.value + ",";
                }
            });

            proVal = proVal.substring(0, proVal.length - 1).split(",");

            for (i = 0; i < hiddenVal.length; i++) {
                var has = true;
                for (j = 0; j < proVal.length; j++) {
                    if (hiddenVal[i] == proVal[j]) {
                        has = false;
                        break;
                    }
                }
                if (has) {
                    delVal += hiddenVal[i] + ",";
                }
            }

            $("#ContentPlaceHolder2_HiddenFiled_add").attr("value", addVal);
            $("#ContentPlaceHolder2_HiddenFiled_del").attr("value", delVal);            
        }


    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <input type="hidden" value="t6" id="pageName" />
    <div class="content-title">
        <h1>
            Customer - Product Category</h1>
    </div>
    <div class="con-nav">
        <a href="Basicinformation.aspx" class="btn"><span class="ui-icon ui-icon-newwin"></span>
            Add New Customer</a>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <ajaxToolkit:ToolkitScriptManager EnablePartialRendering="true" runat="Server" ID="ScriptManager1" />
    <uc1:usermeg ID="usermeg" runat="server"></uc1:usermeg>
    <div class="partWrap">
        <uc1:maintab ID="maintab" runat="server" />
        <div class="clear20">
        </div>
        <div class="productWrap">
            <div class="productTitle">
            </div>
            <div id="id_check_container" class="CheckWrap">
                <ul class="menu1_ul menu1" id="menu1_ul1">
                    <li class="li1">
                        <div class="selected">
                            √</div>
                        <span title="A">Textile & Garment</span></li>
                    <li class="li2">
                        <div class="selected" style="color: #ffffff;">
                            √</div>
                        <span title="B">Electronics, AV & IT</span></li>
                    <li class="li3">
                        <div class="selected" style="color: #ffffff;">
                            √</div>
                        <span title="C">Houseware</span></li>
                    <li class="li4">
                        <div class="selected">
                            √</div>
                        <span title="D">Gifts & Premium</span></li>
                    <li class="li5">
                        <div class="selected">
                            √</div>
                        <span title="E">Others</span></li>
                </ul>
                <h4 class="title20">
                    Main Category</h4>
                <asp:Repeater ID="rp_Cate_All" runat="server" OnItemDataBound="rp_Cate_All_ItemDataBound">
                    <ItemTemplate>
                        <div id="<%# "div_"+Container.DataItem %>" style="display: none;" class="cate_and_sub">
                            <p>
                                <asp:CheckBox ID="cb_all" runat="server" Text="<strong>All</strong>" /><br />
                            </p>
                            <asp:CheckBoxList ID="cbl_cate" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
                                Width="100%" CellPadding="0" CellSpacing="0" CssClass="labelTb">
                            </asp:CheckBoxList>
                            <br />
                            <br />
                            <asp:Repeater ID="rp_cate" runat="server" OnItemDataBound="rp_cate_ItemDataBound">
                                <ItemTemplate>
                                    <p>
                                        <b>
                                            <%#Eval("ProductCat_Eng") + " " + Eval("ProductCat_Chi")%></b><asp:HiddenField ID="hdProductCatUID"
                                                runat="server" Value='<% # Eval("ProductCatUID")%>' />
                                    </p>
                                    <asp:CheckBoxList ID="cbl_subcate" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
                                        Width="100%" CellPadding="0" CellSpacing="0" CssClass="labelTb ProCat">
                                    </asp:CheckBoxList>
                                    <br />
                                </ItemTemplate>
                            </asp:Repeater>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
                <asp:HiddenField runat="server" ID="HiddenField_val" />
                <asp:HiddenField runat="server" ID="HiddenFiled_add" />
                <asp:HiddenField runat="server" ID="HiddenFiled_del" />
            </div>
        </div>
        <div class="clear20">
        </div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <table border="0" cellspacing="0" cellpadding="0" class="btnwrap">
                    <tr>
                        <td class="btnwrapl">
                            <ul class="uibtn">
                                <li>
                                    <asp:LinkButton ID="LbtnSave" runat="server" OnClick="LbtnSave_Click"  OnClientClick='return GetChangeVal()'>Save</asp:LinkButton>
                                </li>
                            </ul>
                        </td>
                        <td class="btnwrapr">
                            <ul class="uibtn">
                                <li>
                                    <asp:LinkButton ID="LbtnQuit" runat="server" OnClick="LbtnQuit_Click">Quit</asp:LinkButton>
                                </li>
                            </ul>
                        </td>
                        <td>
                        </td>
                    </tr>
                </table>
                <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                    <ProgressTemplate>
                        <span style="color: Red;">提交了大量数据，服务器需要处理，请稍等...</span>
                    </ProgressTemplate>
                </asp:UpdateProgress>
                <asp:Label ID="lblMessages" runat="server" Text="" ForeColor="Red"></asp:Label><!--edit by peter 2011-4-12-->
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</asp:Content>
